<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="项目名称" size="small">
            <el-input v-model="searchForm.projectName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="合同编号" size="small">
            <el-input v-model="searchForm.contractCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item prop="leadingDiscipline" label="主导专业" size="small" class="item-inline">
            <el-select v-model="searchForm.leadingDiscipline" placeholder="请选择">
              <el-option value="" label="全部"></el-option>
              <el-option
                v-for="item in parentProfessionList"
                :key="item.professionId"
                :label="item.professionName"
                :value="item.professionId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="other" v-if="roleType===0">
          <el-form-item prop="teamName" label="所属团队" size="small" class="item-inline">
            <el-autocomplete
              v-model="searchForm.teamName"
              :fetch-suggestions="querySearchTeam"
              @select="handleSelectSearchTeam"
              placeholder="请输入内容"
            >
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">

      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :span-arr="spanArr"
      is-span-arr="affiliated"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    getList,
    selectTeam
  } from '@/api/affiliated'
  import {
    getParentProfessionList
  } from '@/api/contractproject'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const time = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'contractCode', label: '合同编号', width: '100', fixed: true },
          { prop: 'projectName', label: '项目名称', width: '350', fixed: true },
          { prop: 'teamName', label: '所属团队', width: '100', fixed: true },
          { prop: 'partyA', label: '甲方单位', width: '150', fixed: true },
          { prop: 'contractAmount', label: '合同金额(万元)', width: '100', fixed: true },
          { prop: 'invoiceTime', label: '开票时间', width: '100', component: time },
          { prop: 'invoiceNumber', label: '发票号码', width: '100' },
          { prop: 'amount', label: '开票金额(万元)', width: '100' },
          { prop: 'billTime', label: '到账时间', width: '100', component: time },
          { prop: 'billAmount', label: '到账金额(万元)', width: '100' },
          { prop: 'taxes', label: '税费(万元)', width: '80' },
          { prop: 'managementCost', label: '管理费(万元)', width: '100' },
          { prop: 'signatureCost', label: '签字费(万元)', width: '100' },
          { prop: 'auditCost', label: '审核费(万元)', width: '100' },
          { prop: 'otherCost', label: '其他费(万元)', width: '100' },
          { prop: 'amountDue', label: '应得金额(万元)', width: '100' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        tenderNames: [],
        infoTitles: [],
        listLoading: true,
        dialogTitle: '新增社保',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          rank: [
            { required: true, message: '请输入职级', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '请选择性别', trigger: 'change' }
          ],
          originalDepartmentId: [
            { required: true, message: '请选择调出部门', trigger: 'change' }
          ],
          originalPosition: [
            { required: true, message: '请输入原职务', trigger: 'blur' }
          ]
        },
        visible: false,
        parentProfessionList: [],
        spanArr: []
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.getParentProfessionList()
      this.fetchData()
    },
    computed: {
      global() {
        return global
      },
      roleType() {
        return this.$store.state.user.roleType
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.getSpanArr(this.tableData)
          this.listLoading = false
        })
      },
      getParentProfessionList() {
        getParentProfessionList().then(response => {
          this.parentProfessionList = response.responseBody
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      getSpanArr(data) {
        this.spanArr = [] // 定义在vue的data中
        if (data == null) {
          return
        }
        for (var i = 0; i < data.length; i++) {
          if (i === 0) {
            this.spanArr.push(1)
            this.pos = 0
          } else {
            if (data[i].projectId === data[i - 1].projectId) {
              this.spanArr[this.pos] += 1
              this.spanArr.push(0)
            } else {
              this.spanArr.push(1)
              this.pos = i
            }
          }
        }
        console.log(this.spanArr)
      },
      querySearchTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.searchForm.teamId
        if (queryString) {
          const params = {
            teamName: queryString
          }
          selectTeam(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.teamName
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectSearchTeam(item) {
        this.$set(this.searchForm, 'teamId', item.teamId)
        this.$set(this.searchForm, 'teamName', item.teamName)
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
